<template>
  <el-dialog
    customClass="elc-dialog"
    v-dialogDrag
    :close-on-click-modal="false"
    :title="title"
    width="680px"
    :visible.sync="dialogVisible"
    @close="cancel"
    append-to-body
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="110px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="上级菜单" prop="parentId">
            <treeselect
              v-model="form.parentId"
              :options="menuOptions"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="选择上级菜单"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="form.type">
              <el-radio label="0">菜单</el-radio>
              <el-radio label="1">按钮</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="form.type != '1'">
          <el-form-item label="图标" prop="menuIcon" style="position: relative">
            <el-popover
              placement="bottom-start"
              width="460"
              trigger="click"
              @show="$refs['iconSelect'].reset()"
            >
              <IconSelect
                ref="iconSelect"
                @selected="selected"
                :active-icon="form.menuIcon"
              />
              <el-input
                slot="reference"
                v-model="form.menuIcon"
                placeholder="点击选择图标"
                clearable
              >
                <svg-icon
                  v-if="form.menuIcon"
                  slot="prefix"
                  :icon-class="form.menuIcon"
                  style="width: 20px; height: 40px"
                />
                <i v-else slot="prefix" class="el-icon-search el-input__icon" />
              </el-input>
            </el-popover>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称" prop="menuName">
            <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示排序" prop="menuOrderNum">
            <el-input-number
              v-model="form.menuOrderNum"
              controls-position="right"
              :min="0"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="form.type != '1'">
          <el-form-item prop="menuRedirect">
            <span slot="label">
              <el-tooltip
                content="访问的路由地址，如：`user`，如外网地址需内链访问则以`http(s)://`开头"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              路由地址
            </span>
            <el-input
              v-model="form.menuRedirect"
              placeholder="请输入路由地址"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="form.type != '0'">
          <el-form-item prop="menuEnName" label="标识">
            <el-input v-model="form.menuEnName" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveOrUpdate, allMenu } from "@/api/menu";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";

export default {
  name: "Menu",
  components: { Treeselect, IconSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        menuName: undefined,
        visible: undefined,
      },
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "上级菜单不能为空", trigger: "blur" },
        ],
        type: [{ required: true, message: "类型不能为空！", trigger: "blur" }],
        menuName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        menuRedirect: [
          { required: true, message: "路由不能为空", trigger: "blur" },
        ],
        menuEnName: [
          { required: true, message: "标识不能为空", trigger: "blur" },
        ],
      },
      title: "",
      dialogVisible: false,
      form: {
        menuName: "",
        menuEnName: "",
        parentId: "",
        menuOrderNum: "",
        menuRedirect: "",
        menuIcon: "",
        type: "1",
      },
    };
  },
  created() {},
  methods: {
    show(obj) {
      this.getTreeselect();
      this.title = obj.title;
      this.form = obj.data ? obj.data : this.form;
      this.dialogVisible = true;
    },
    // 选择图标
    selected(name) {
      this.form.menuIcon = name;
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.menuName,
        children: node.children,
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      allMenu().then((response) => {
        this.menuOptions = [];
        const menu = { id: 0, menuName: "主类目", children: [] };
        menu.children = response.data;
        this.menuOptions.push(menu);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        menuName: "",
        menuEnName: "",
        parentId: "",
        menuOrderNum: "",
        menuRedirect: "",
        menuIcon: "",
        type: "1",
      };
      this.resetForm("form");
      this.$emit("closeDialog");
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.menuRedirect) {
            Object.assign(this.form, {
              menuEnName: this.form.menuRedirect,
            });
          }
          saveOrUpdate(this.form).then(() => {
            this.dialogVisible = false;
          });
        }
      });
    },
  },
};
</script>
